package com.doublefish.doublefishrobot.ui.home.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;

import com.doublefish.doublefishrobot.R;

/**
 * 作者:dugaofeng on 2024/12/23 16:01
 */
public class ModeSettingItemDoubleSlider extends RelativeLayout {

    private Context mContext;

    private View bgLine;
    private View leftLine;   //最左边占位的view
//    private TextView leftSeekTv;
    private CardView leftSeekTv;

    private View rightLine;    //中间占位的view
//    private TextView rightSeekTv;
    private CardView rightSeekTv;


    private static final int DEFAULT_MAX_WIDTH = 10000;
    private int maxWidth = DEFAULT_MAX_WIDTH;
    private int isMoving = 0;   // 是否移动中  0.没有移动  1.左边滑块  2.右边滑块
    private int lastX;
    private int lastLeft;     //左滑块的位置
    private int lastRight;  //右滑块的位置

    private int curMin = 1;
    private int curMax = 100;
    private int min = 1;     //随便给个默认值，防止计算时除数为0
    private int max = 100;   //随便给个默认值，防止计算时除数为0

    private boolean windowFocusChanged = false;

    private OnModeSettingItemDoubleSliderListner onModeSettingItemDoubleSliderListner;

    public ModeSettingItemDoubleSlider(@NonNull Context context) {
        this(context, null);
    }

    public ModeSettingItemDoubleSlider(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ModeSettingItemDoubleSlider(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }

    private void initView() {
        inflate(getContext(), R.layout.dialog_mode_setting_double_slider, this);

        bgLine = findViewById(R.id.view_bg);
        leftLine = findViewById(R.id.view_offset1);
        leftSeekTv = findViewById(R.id.tv_seek1);
        rightLine = findViewById(R.id.view_offset2);
        rightSeekTv = findViewById(R.id.tv_seek2);
    }

    //从外部传一个宽度进去
    public void setMaxWidth(int width) {
        this.maxWidth = width;
        updateOffsetViewWidth();
    }

    public void setValue(int curMin, int curMax, int min, int max) {
        this.curMin = curMin;
        this.curMax = curMax;
        this.min = min;
        this.max = max;

        updateOffsetViewWidth();
    }

    private void updateOffsetViewWidth() {
        if (maxWidth == DEFAULT_MAX_WIDTH) {
            return;
        }
        if (max - min == 0) {
            return;
        }
        int progressLeft = (curMin - min) * 100 / (max - min);
        int widthLeft = maxWidth * progressLeft / 100;
        ViewGroup.LayoutParams paramsLeft = leftLine.getLayoutParams();
        paramsLeft.width = widthLeft;
        leftLine.setLayoutParams(paramsLeft);
        lastLeft = widthLeft;

        int progressRight = (curMax - min) * 100 / (max - min);
        int widthRight = maxWidth * progressRight / 100;
        ViewGroup.LayoutParams paramsRight = rightLine.getLayoutParams();
        paramsRight.width = widthRight;
        rightLine.setLayoutParams(paramsRight);
        lastRight = widthRight;
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        int action = event.getAction();

        if (action == MotionEvent.ACTION_DOWN) {
            //点击的位置
            int x = (int)event.getX();
            int y = (int)event.getY();
            //按钮的位置 尺寸
            int seekTvWidth = rightSeekTv.getMeasuredWidth();  //宽高
            int rightSeekTvX = (int)rightSeekTv.getX();
            int rightSeekTvY = (int)rightSeekTv.getY();

            int leftSeekTvX = (int)leftSeekTv.getX();
            int leftSeekTvY = (int)leftSeekTv.getY();
            
            //判断点击的位置是否在按钮的范围
            if (x > rightSeekTvX && x < rightSeekTvX + seekTvWidth && y > rightSeekTvY && y < rightSeekTvY + seekTvWidth) {
                isMoving = 2;
                lastX = x;
            } else if (x > leftSeekTvX && x < leftSeekTvX + seekTvWidth && y > leftSeekTvY && y < leftSeekTvY + seekTvWidth) {
                isMoving = 1;
                lastX = x;
            }
        } else if (action == MotionEvent.ACTION_MOVE && isMoving != 0) {
            int x = (int)event.getX();
            int offset = x - lastX;
            if (offset == 0) {
                return true;
            }

            if (isMoving == 1) {
                //左边的滑块
                if (offset < 0) {
                    leftSeekTv.bringToFront();
                    //往左滑
                    ViewGroup.LayoutParams params = leftLine.getLayoutParams();
                    int width = params.width + offset;
                    if (width<0) {
                        width = 0;
                    } else if (width > maxWidth) {
                        width = maxWidth;
                    }
                    params.width = width;
                    leftLine.setLayoutParams(params);
                    lastLeft = width;
                    //计算当前值
                    int progress = width * 100 / maxWidth;
                    curMin = (max - min) * progress / 100 + min;
                    if (this.onModeSettingItemDoubleSliderListner != null) {
                        this.onModeSettingItemDoubleSliderListner.onValueChanged(curMin, curMax);
                    }
                } else {
                    //往右滑
                    ViewGroup.LayoutParams params = leftLine.getLayoutParams();
                    int width = params.width + offset;
                    if (width<0) {
                        width = 0;
                    } else if (width > maxWidth) {
                        width = maxWidth;
                    }
                    if (width <= lastRight) {
                        //滑动位置没有超过右边的滑动
                        leftSeekTv.bringToFront();

                        params.width = width;
                        leftLine.setLayoutParams(params);
                        lastLeft = width;
                        //计算当前值
                        int progress = width * 100 / maxWidth;
                        curMin = (max - min) * progress / 100 + min;
                        if (this.onModeSettingItemDoubleSliderListner != null) {
                            this.onModeSettingItemDoubleSliderListner.onValueChanged(curMin, curMax);
                        }
                    } else  {
                        //滑动位置超过了右边的滑动
                        rightSeekTv.bringToFront();

                        isMoving = 2;
                        ViewGroup.LayoutParams paramsRight = rightLine.getLayoutParams();
                        paramsRight.width = width;
                        rightLine.setLayoutParams(paramsRight);
                        lastRight = width;
                        //计算当前值
                        int progress = width * 100 / maxWidth;
                        curMax = (max - min) * progress / 100 + min;
                        if (this.onModeSettingItemDoubleSliderListner != null) {
                            this.onModeSettingItemDoubleSliderListner.onValueChanged(curMin, curMax);
                        }
                    }
                }
            } else if (isMoving == 2) {
                //右边的滑块
                if (offset < 0) {
                    //往左滑
                    ViewGroup.LayoutParams params = rightLine.getLayoutParams();
                    int width = params.width + offset;
                    if (width<0) {
                        width = 0;
                    } else if (width > maxWidth) {
                        width = maxWidth;
                    }
                    if (width >= lastLeft) {
                        //滑动位置没有超过了左边的滑动
                        rightSeekTv.bringToFront();

                        params.width = width;
                        rightLine.setLayoutParams(params);
                        lastRight = width;
                        //计算当前值
                        int progress = width * 100 / maxWidth;
                        curMax = (max - min) * progress / 100 + min;
                        if (this.onModeSettingItemDoubleSliderListner != null) {
                            this.onModeSettingItemDoubleSliderListner.onValueChanged(curMin, curMax);
                        }
                    } else {
                        //滑动位置超过了左边的滑动
                        leftSeekTv.bringToFront();

                        isMoving = 1;
                        ViewGroup.LayoutParams paramsLeft = leftLine.getLayoutParams();
                        paramsLeft.width = width;
                        leftLine.setLayoutParams(paramsLeft);
                        lastLeft = width;
                        //计算当前值
                        int progress = width * 100 / maxWidth;
                        curMin = (max - min) * progress / 100 + min;
                        if (this.onModeSettingItemDoubleSliderListner != null) {
                            this.onModeSettingItemDoubleSliderListner.onValueChanged(curMin, curMax);
                        }
                    }
                } else {
                    //往右滑
                    rightSeekTv.bringToFront();

                    ViewGroup.LayoutParams params = rightLine.getLayoutParams();
                    int width = params.width + offset;
                    if (width<0) {
                        width = 0;
                    } else if (width > maxWidth) {
                        width = maxWidth;
                    }
                    params.width = width;
                    rightLine.setLayoutParams(params);
                    lastRight = width;
                    //计算当前值
                    int progress = width * 100 / maxWidth;
                    curMax = (max - min) * progress / 100 + min;
                    if (this.onModeSettingItemDoubleSliderListner != null) {
                        this.onModeSettingItemDoubleSliderListner.onValueChanged(curMin, curMax);
                    }
                }
            }
            lastX = x;
        } else {
            isMoving = 0;
        }
        return true;
    }

    public void setOnModeSettingItemDoubleSliderListner(OnModeSettingItemDoubleSliderListner onModeSettingItemDoubleSliderListner) {
        this.onModeSettingItemDoubleSliderListner = onModeSettingItemDoubleSliderListner;
    }

    public interface OnModeSettingItemDoubleSliderListner {
        void onValueChanged(int curMin, int curMax);
    }
}
